package ${package}.commons;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.joda.time.DateTime;
import org.joda.time.Period;

public class Utils {

	public static String generateTimeGap(long beginTime) {

		String output = "";

		DateTime current = new DateTime();
		DateTime post = new DateTime(beginTime);

		Period p = new Period(post, current);
		int month = p.getMonths();
		int week = p.getWeeks();
		int day = p.getDays();
		int hour = p.getHours();
		int mins = p.getMinutes();
		if (month > 0)
			output += month + "个月 ";
		if (week > 0)
			output += week + "星期 ";
		if (day > 0)
			output += day + "天 ";
		if (hour > 0)
			output += hour + "小时 ";
		if (mins > 0)
			output += mins + "分钟 ";
		output += "前";
		return output;
	}

	/* replace multiple whitespaces between words with single blank */
	public static String trim(String source) {
		return source.replaceAll("\\b\\s{2,}\\b", " ");
	}

	public static String regexReplace(String source, String patternString,
			String replacementString, String suffix) {
		// Compile regular expression
		Pattern pattern = Pattern.compile(patternString);

		// Replace all occurrences of pattern in input
		Matcher matcher = pattern.matcher(source);
		String replacedString = matcher.replaceAll(replacementString);
		return replacedString + suffix;
	}
}
